package com.android.assignment.taskreminder;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class TaskReminderProvider extends ContentProvider {

	// Database constants
	private static final String DATABASE_NAME = "task_reminder";
	private static final String DATABASE_TABLE = "task";
	private static final int DATABASE_VERSION = 1;

	// Database columns
	public static final String COLUMN_ROWID = "_id";
	public static final String COLUMN_TITLE = "title";
	public static final String COLUMN_CONTENT = "content";
	public static final String COLUMN_DATE_TIME = "remind_date_time";
	public static final String COLUMN_STATUS = "status";
	public static final String COLUMN_PRIORITY = "priority";

	// Create table SQL statement
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (" + COLUMN_ROWID
			+ " integer primary key autoincrement, " + COLUMN_TITLE
			+ " text not null, " + COLUMN_CONTENT + " text not null, "
			+ COLUMN_DATE_TIME + " integer not null, " + COLUMN_STATUS
			+ " text not null, " + COLUMN_PRIORITY + " integer);";

	// Content provider Uri and Authority
	public static String AUTHORITY = "com.android.assignment.taskreminder.TaskReminderProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/reminder");

	// MIME type
	public static final String REMINDERS_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
			+ "/vnd.com.android.assignment.taskreminder.reminder";
	public static final String REMINDER_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
			+ "/vnd.com.android.assignment.taskreminder.reminder";

	// UriMatcher
	private static final int LIST = 0;
	private static final int ITEM = 1;
	private static final UriMatcher sUriMatcher = buildUriMatcher();

	private static UriMatcher buildUriMatcher() {
		UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(AUTHORITY, "reminder", LIST);
		matcher.addURI(AUTHORITY, "reminder/#", ITEM);
		return matcher;
	}

	private SQLiteDatabase myDatabase;

	// Open database to read and write
	@Override
	public boolean onCreate() {
		myDatabase = new DatabaseHelper(getContext()).getWritableDatabase();
		return true;
	}

	// Query all or one row
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		String[] projection1 = new String[] { COLUMN_ROWID, COLUMN_TITLE,
				COLUMN_CONTENT, COLUMN_DATE_TIME, COLUMN_STATUS };

		Cursor c;
		switch (sUriMatcher.match(uri)) {
		case LIST:
			c = myDatabase.query(DATABASE_TABLE, projection1, null, null, null,
					null, null);
			break;
		case ITEM:
			c = myDatabase.query(DATABASE_TABLE, projection1, COLUMN_ROWID
					+ "=?",
					new String[] { Long.toString(ContentUris.parseId(uri)) },
					null, null, null, null);
			if (c != null && c.getCount() > 0) {
				c.moveToFirst();
			}
			break;
		default:
			throw new IllegalArgumentException("Unknown Uri: " + uri);
		}
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	// Insert row
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		values.remove(COLUMN_ROWID);
		long id = myDatabase.insertOrThrow(DATABASE_TABLE, null, values);
		getContext().getContentResolver().notifyChange(uri, null);
		return ContentUris.withAppendedId(uri, id);
	}
	
	// Update rows
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int count = myDatabase.update(DATABASE_TABLE, values, COLUMN_ROWID
				+ "=?",
				new String[] { Long.toString(ContentUris.parseId(uri)) });
		if(count > 0)
			getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	// Delete rows
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		int count = myDatabase.delete(DATABASE_TABLE, COLUMN_ROWID + "=?",
				new String[] { Long.toString(ContentUris.parseId(arg0)) });
		if (count > 0)
			getContext().getContentResolver().notifyChange(arg0, null);
		return count;
	}

	// get MIME type
	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
		case LIST:
			return REMINDERS_MIME_TYPE;
		case ITEM:
			return REMINDER_MIME_TYPE;
		default:
			throw new IllegalArgumentException("Unknown Uri: " + uri);
		}
	}
	
	// Database helper class
	private class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			throw new UnsupportedOperationException();
		}

	}

}
